//侧手风琴导航栏
var as = document.querySelectorAll("li.nav-item>a");
// console.log(as);
for (var a of as) {
    a.onclick = function () {
        if (this.className == 'open') {
            this.className = '';
        } else {
            var openSpan = document.querySelector("li.nav-item>a.open");
            if (openSpan != null) {
                openSpan.className = '';
            }
            this.className = 'open';
        }
    };
};

//用户名检验
function name_blur() {
    let aname = username.value;

    if (!aname) {
        user_msg.innerHTML = "用户名不能为空！";
    } else {
        user_msg.innerHTML = "";
    }
}

//密码检验
function pwd_blur() {
    let apwd = password.value;

    if (!apwd) {
        password_msg.innerHTML = "密码不能为空！";
    } else {
        password_msg.innerHTML = "";
    }
}

//ajax登录
function login() {
    name_blur();
    pwd_blur();

    if (user_msg.innerHTML === '' && password_msg.innerHTML === '') {
        let _aname = username.value;
        let _apwd = password.value;

        ajax({
            url: "/admin/login",
            type: 'get',
            data: `${_aname}&${_apwd}`,
            success: function (data) {
                if (data == 1) {
                    alert('登录成功');
                    location.replace('/admin/admin.html');
                } else {
                    user_msg.innerHTML = "用户名或密码错误";
                }
            },
            error: function (e) {
                console.log(e);
            }
        })
    }
}

// 用户列表
function user_list() {
    let body = document.querySelector('tbody');

    ajax({
        url: "/admin/user/list",
        type: 'get',
        data: '',
        success: function (data) {
            data = JSON.parse(data);
            // console.log(data);
            if (data == 0) {
                return;
            } else {
                let str = '';
                for (let key in data) {
                    let sex = '保密';
                    if (data[key].gender == 1) {
                        sex = '男';
                    } else if (data[key].gender == 0) {
                        sex = '女';
                    }

                    str += `<tr>
                                <td>${parseInt(key) + 1}</td>
                                <td>${data[key].uname}</td>
                                <td>${data[key].phone}</td>
                                <td>${sex}</td>
                                <td>${data[key].email}</td>
                                <td>
                                    <a href="javascript:;" onclick="get_edit()">
                                        <img src="../images/admin/icon/Write.png">修改
                                    </a>
                                    &nbsp;|&nbsp;
                                    <a href="javascript:;" onclick="user_delete(${data[key].uid})">
                                        <img src="../images/admin/icon/Garbage.png">删除
                                    </a>
                                </td>
                            </tr>`;
                }
                body.innerHTML = str;
            }
        },
        error: function (e) {
            console.log(e);
        }
    })
}

//判断用户名是否重复
function check() {
    let _name = uname.value;

    if (!_name) {
        uname_msg.innerHTML = '不能为空';
    } else if (!(/^1[3-9]\d{9}$/.test(_name))) {
        uname_msg.innerHTML = '手机号不正确';
    } else {
        ajax({
            url: '/user/getUname',
            type: 'get',
            data: `${_name}`,
            success: function (data) {
                if (data == 1) {
                    return uname_msg.innerHTML = '手机号码已被注册';
                }
                uname_msg.innerHTML = '';
            },
            error: function (e) {
                console.log(e);
            }
        });
    }
}

//判断密码
function check_pwd() {
    let _pwd = upwd.value;

    if (!_pwd) {
        upwd_msg.innerHTML = '不能为空';
    } else if (!(/^[A-Za-z0-9]{6,10}$/.test(_pwd))) {
        upwd_msg.innerHTML = '密码为字母或数字，6到10位';
    } else {
        upwd_msg.innerHTML = '';
    }
}

//注册用户
function register() {
    check();
    check_pwd();

    if (uname_msg.innerHTML === '' && upwd_msg.innerHTML === '') {
        let _uname = uname.value;
        let _upwd = upwd.value;

        ajax({
            url: '/user/reg',
            type: 'post',
            data: `uname=${_uname}&upwd=${_upwd}&phone=${_uname}`,
            success: function (data) {
                if (data == 1) {
                    alert('注册成功');
                    location.reload();
                } else {
                    alert('注册失败');
                }
            },
            error: function (e) {
                console.log(e);
            }
        });
    }
}

//用户删除
function user_delete(id) {
    if (confirm('是否确认删除？')) {
        ajax({
            url: '/admin/user_delete',
            type: 'post',
            data: `uid=${id}`,
            success: function (data) {
                if (data == 1) {
                    alert('删除成功');
                    location.reload();
                } else {
                    alert('删除失败');
                }
            },
            error: function (e) {
                console.log(e);
            }
        });
    }
}

//动画字体
function show(){
    //获取标题
    const title = document.querySelector("#admin_title");
    //console.log([...title.textContent]); //['x','k','x','.',....]
    [...title.textContent].reduce((pre, value, index) => {
        // console.log(pre,value,index);
        pre == index && (title.innerHTML = "");
        //创建元素span
        let span = document.createElement('span');
        //将解构的字符添加进span
        span.innerHTML = value;
        //在title中插入子元素span
        title.appendChild(span);

        //span监听事件，添加样式
        span.addEventListener("mouseover", function () {
            this.classList.add('color');
        });
        span.addEventListener("animationend", function () {
            this.classList.remove('color');
        });
    }, 0);
}
